package com.gusto.engine.spring.parsers;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import com.gusto.engine.semsim.measures.impl.PropertySimilarity;
import com.gusto.engine.semsim.measures.impl.PropertySimilarityParam;

public class ParserPropertySimilarity extends AbstractBeanDefinitionParser {
	
	@SuppressWarnings("unchecked")
	protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
		BeanDefinitionBuilder propertyDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(PropertySimilarity.class);
		
		
		BeanDefinitionBuilder paramDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(PropertySimilarityParam.class);
		
		if (element.hasAttribute("type")) {
			String type = element.getAttribute("type");
			paramDefinitionBuilder.addPropertyValue("type", type);
		}
		if (element.hasAttribute("similarity")) {
			String similarity = element.getAttribute("similarity");
			paramDefinitionBuilder.addPropertyReference("similarity", similarity);
		}
		if (element.hasAttribute("property")) {
			String property = element.getAttribute("property");
			paramDefinitionBuilder.addPropertyValue("property", property);
		}
		
		propertyDefinitionBuilder.addPropertyValue("param", paramDefinitionBuilder.getBeanDefinition());
		
		return propertyDefinitionBuilder.getBeanDefinition();
	}
}
